<?php

class MValidator
{
	/**
	 * 验证不能为空
	 * @param string $value
	 * @return boolean
	 */
    public static function required($value)
    {
    	if (is_string($value)) {
    		$value = trim($value);
    	}
    	return (!empty($value));
    }
    
    /**
     * 验证群组至少包含一项
     * @param string $value
     * @return boolean
     */
    public static function groupRequired($value)
    {
    	foreach ($value as $item) {
    		if (trim($item) != '') {
    			return true;
    		}
    	}
    	return false;
    }
    
    /**
     * 验证群组最低包含项
     * @param string $value
     * @param int $size
     * @return boolean
     */
    public static function minCheckbox($value, $size)
    {
    	$size = (int) $size;
    	$checked = 0;
    	foreach ($value as $item) {
    		if (trim($item) != '') {
    			$checked++;
    		}
    	}
    	return ($checked >= $size);
    }
    
    /**
     * 验证群组最大包含项
     * @param string $value
     * @param int $size
     * @return boolean
     */
    public static function maxCheckbox($value, $size)
    {
    	$size = (int) $size;
    	$checked = 0;
    	foreach ($value as $item) {
    		if (trim($item) != '') {
    			$checked++;
    		}
    	}
    	return ($checked <= $size);
    }
    
    /**
     * 验证最小长度
     * @param string $value
     * @param int $size
     * @param string $encoding 字符编码
     * @return boolean
     */
    public static function minSize($value, $size, $encoding = 'utf-8')
    {
    	$size = (int) $size;
    	return (mb_strlen($value, $encoding) >= $size);
    }
    
    /**
     * 验证最大长度
     * @param string $value
     * @param int $size
     * @param string $encoding 字符编码
     * @return boolean
     */
    public static function maxSize($value, $size, $encoding = 'utf-8')
    {
    	$size = (int) $size;
    	return (mb_strlen($value, $encoding) <= $size);
    }
    
    /**
     * 验证最小值
     * @param string $value
     * @param int $min
     * @return boolean
     */
    public static function min($value, $min)
    {
    	$min = (int) $min;
    	$value = (int) $value;
    	return ($value >= $min);
    }
    
    /**
     * 验证最大值
     * @param string $value
     * @param int $max
     * @return boolean
     */
    public static function max($value, $max)
    {
    	$max = (int) $max;
    	$value = (int) $value;
    	return ($value <= $max);
    }
    
    /**
     * 验证是否数字
     * @param string $value
     * @param int $min
     * @return boolean
     */
    public static function number($value)
    {
    	return is_numeric($value);
    }
    
    /**
     * 验证电话号码
     * @param string $value
     * @return bool
     */
    public static function phone($value)
    {
    	$pattern = '/^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/';
    	return preg_match($pattern, $value);
    }
    
    /**
     * 验证邮箱地址
     * @param string $value
     * @return bool
     */
    public static function email($value)
    {
    	$pattern = '/^[a-z][a-z\d]*?[a-z\d-_]*?[a-z\d]\@[a-z\d]+?[a-z\d-]*?[a-z\d]+?\.[a-z]+?$/i';
    	return preg_match($pattern, $value);
    }
    
    /**
     * 验证url地址
     * @param string $value
     * @return bool
     */
    public static function url($value)
    {
    	$pattern = '/^https?:\/\/([a-z\d]*?[a-z\d-]*?[a-z\d]+?\.)*[a-z\d]+?[a-z\d-]*?[a-z\d]+?\.[a-z]+?[\S]*$/i';
    	return preg_match($pattern, $value);
    }
    
    /**
     * 验证英文字母
     * @param string $value
     * @return bool
     */
    public static function english($value)
    {
    	$pattern = '/^[a-z]+$/i';
    	return preg_match($pattern, $value);
    }
    
    /**
     * 验证中文
     * @param string $value
     * @return bool
     */
    public static function chinese($value)
    {
    	$pattern = '/^[\x{4e00}-\x{9fa5}]+$/u';
    	return preg_match($pattern, $value);
    }
    
    /**
     * 验证身份证号码
     * @param string $value
     * @return bool
     */
    public static function id($value)
    {
    
    }
    
    /**
     * 验证日期格式
     * @param string $value
     * @return bool
     * YYYY-MM-DD
     */
    public static function date($value)
    {
    	$pattern = '/^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/';
    	return preg_match($pattern, $value);
    }
    
    /**
     * 验证邮编
     * @param string $value
     * @return bool
     */
    public static function zipcode($value)
    {
    
    }
    
    /**
     * 验证QQ号
     * @param string $value
     * @return bool
     */
    public static function qq($value)
    {
    
    }
    
    public static function equals($value, $source)
    {
    	return ($value == $source);
    }
    
    /**
     * 验证是否存在指定值中
     * @param string $value
     * @param array $rules
     * @return boolean
     */
    public static function in($value, $rules)
    {
    	return in_array($value, $rules);
    }
    
    public static function run($name, $params)
    {
    	return call_user_func_array(array('self', $name), $params);
    }
    
    public static function messages($name, $title = '')
    {
    	$msg =  array(
    		'required' => '不能为空',
    		'groupRequired' => '至少选中一项',
    		'minCheckbox' => '低于最小选中项',
    		'maxCheckbox' => '高于最大选中项',
    		'minSize' => '低于最小字符长度',
    		'maxSize' => '高于最大字符长度',
    		'min' => '低于最小数值',
    		'max' => '高于最大数值',
    		'number' => '必须是数字',
    		'phone' => '格式不正确',
    		'email' => '格式不正确',
    		'url' => '格式不正确',
    		'english' => '只能是英文字母',
    		'chinese' => '只能是中文',
    		'date' => '格式不正确'
    	);
    	return $title . $msg[$name];
    }
    
    /*
    public static function run($attributes, &$source)
    {
        foreach ($attributes as $name => $item) {
            $method = $item[0];
            if (!call_user_func(array('self', $item[0]), $source[$name])) {
                return $item[1];
            }
        }
    }
    */
}